#!/usr/bin/php
<?php
//error_reporting(E_ERROR | E_PARSE);

// set start time
$mtime = microtime();
$mtime = explode(' ', $mtime);
$mtime = $mtime[1] + $mtime[0];
$starttime = $mtime;

if (isset($argv[1])) { 
	$datedir = $argv[1];
	echo "\nDate was entered on the commandline: ".$datedir; } 
else {
	$datedir = date('Y-m-d'); 
	echo "\nDate was not entered on the commandline assuming current date: ".$datedir; }

// Open a known directory, and proceed to read its contents
$dir = "/sd/customer/apnnz/sampling/".$datedir;
echo "\nProcessing Data in: ".$dir."\n";

// open this directory 
$myDirectory = opendir($dir);

// get each entry
while($entryName = readdir($myDirectory)) {
	$dirArray[] = $entryName;
}

$indexCount	= count($dirArray);
$y=1;

echo "\nDIRECTORY LISTING OF THE FILES\n";
for($index=0; $index < $indexCount; $index++) {
        if (substr("$dirArray[$index]", 0, 17) == "apnnz.sampling.20"){ // don't list hidden files
		print($dirArray[$index]);
		$filename[$y] = $dirArray[$index];
		$y++;
		print("\n");
	    }
	}

$filecount = count($filename);

// Parse the files
echo "\nPARSING THE FILES\n";
$i=1;
while($i <= $filecount){
	$parse_cmd = "zcat ".$dir."/".$filename[$i]." | sed -e 's/ /_/g' | sed -e 's/,/ /g' | sed -e 's/,,/ /g' | awk '{print $12,$13}' | sort | grep -f '/sv/customer/apnnz/conf/styareas.txt' |  awk '{print $1}' | sed -e 's/_/ /g' | sed -e 's/ /\\n/g' | sort > /tmp/".$datedir."_file".$i.".txt";
	echo $parse_cmd;
	echo "\n";
	shell_exec($parse_cmd);
	$i++;
}

// Cat all of the files together
echo "\nCONCATENATE THE FILES TOGETHER\n";
$cat_cmd = "cat /tmp/".$datedir."_file1.txt";
$i=2;
while($i <= $filecount){
	$cat_cmd = $cat_cmd." /tmp/".$datedir."_file".$i.".txt";
	$i++;
}

	$cat_cmd = $cat_cmd." > /tmp/".$datedir."_keywords.all";
	echo $cat_cmd."\n";
	shell_exec($cat_cmd);
	echo "\n";

// Sort the single file
echo "\nSORT THE BIG FILE\n";
	$sort_cmd = "sort -n /tmp/".$datedir."_keywords.all | uniq -c | sort -n -r > /tmp/".$datedir."_keywords.txt";
	echo $sort_cmd."\n";
	shell_exec($sort_cmd);

// Remove all the extra files
echo "\nREMOVING THE FILES\n";
$i=1;
while($i <= $filecount){
	$rm_cmd = "rm /tmp/".$datedir."_file".$i.".txt";
	echo $rm_cmd;
	shell_exec($rm_cmd);
	echo "\n";
	$i++;
}
	$rm_keywords_cmd = "rm /tmp/".$datedir."_keywords.all";
	echo $rm_keywords_cmd."\n";
	shell_exec($rm_keywords_cmd);

	$gzip_keywords_cmd = "gzip /tmp/".$datedir."_keywords.txt";
	echo $gzip_keywords_cmd."\n";
	shell_exec($gzip_keywords_cmd);

// Check the file is the correct size
if(file_exists("/tmp/".$datedir."_keywords.txt.gz")) {
	echo "\nValidating file: /tmp/".$datedir."_keywords.txt.gz\n";
	$file_size = filesize("/tmp/".$datedir."_keywords.txt.gz");
	if($file_size < 10000) {
		echo "\nFAILURE!!! The file doesn't appear to contain enough data. The zipped file size is: ".$file_size."\n";
		echo "\nRemoving the file.\n";
		unlink("/tmp/".$datedir."_keywords.txt.gz");
	}
	else {
		echo "\nSUCCESS!!! The file appears to be the proper size. The zipped file size is: ".$file_size."\n";

		// Since the file appears to be valid, move the final sipped file to shared data
		echo "\nMOVE THE FILE TO SHARED VOLUME\n";
		$move_cmd = "mv /tmp/".$datedir."_keywords.txt.gz /sv/customer/apnnz/keyword_data/";
		echo $move_cmd."\n";
		shell_exec($move_cmd);
	}
}
?>

